<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body {
            margin: 0;
        }
        div {
            width: 100px;
            height: 100px;
            background-color: red;
            opacity: 1;
        }

    </style>
</head>
<body>
    <div></div>
    <script>
        var div = document.getElementsByTagName('div')[0];
        var timer = null;
        div.onclick = function() {
            startMove(this, 50);
        }

        function startMove(dom, target) {
            clearInterval(timer);
            var speed = null;
            dom.style.opacity = getStyle(dom, 'opacity');
            timer = setInterval(function(){
                speed = (target - parseFloat(dom.style.opacity) * 100) / 7;
                // 透明度处理时，扩大一百倍处理
                speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);
                if(parseFloat(dom.style.opacity) * 100 == target) {
                    clearInterval(timer);
                }else {
                    dom.style.opacity = (parseFloat(dom.style.opacity) * 100 + speed) / 100 + ''; 
                }
            }, 30);
        }

        function getStyle(dom, prop) {
            if(window.getComputedStyle) {
                return window.getComputedStyle(dom, null)[prop];
            }else{
                return dom.currentStyle[prop];
            }
        }

    </script>
</body>
</html>